﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UNDP_Projects.Data_Access;

namespace UNDP_Projects.CO.Edit
{
    public partial class AddCPDOutputIndicator : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            /*if (User.IsInRole("Project"))
            {
                MasterPageFile = "~/MasterPages/Project.Master";
            }
            if (User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/Programme.Master";
            }
            if (User.IsInRole("Administrator"))
            {
                MasterPageFile = "~/MasterPages/CO.Master";
            }
            if (User.IsInRole("Project") && User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/ProgProject.Master";
            }*/
        }

        UNDPEntities db = new UNDPEntities();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                string ID = IndicatorIDTextBox.Text.Trim();

                var AcpdIndicator = (from c in db.cpd_output_indicators where 
                                         c.cpd_output_indicator_id == ID select c).FirstOrDefault();

                if (AcpdIndicator == null)
                {
                    //============================================+
                    //  Add a new CPD Output Indicator Record     |
                    //============================================+
                    cpd_output_indicators newIndicator = new cpd_output_indicators();
                    newIndicator.cpd_output_indicator_id = ID;
                    newIndicator.cpd_output_id = CPDOutputList.SelectedValue.Trim();
                    newIndicator.cpd_output_indicator_question = IndicatorTextBox.Text.Trim();

                    db.cpd_output_indicators.AddObject(newIndicator);
                    db.SaveChanges();

                    error.Text = "SAVED";
                    error.Visible = true;

                    IndicatorIDTextBox.Text = null;
                    IndicatorTextBox.Text = null;
                    CPDOutputList.SelectedValue = null;
                }
                else
                {
                    error.Text = "That CPD Output Indicator ID already exists";
                    error.Visible = true;
                }
            }
            catch (Exception ex)
            {
                error.Text = "Database Error: " + ex.Message + "<br /> " + ex.StackTrace;
                error.Visible = true;
            }
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/CO/Cpd_Output_Indicators.aspx", true);
        }
    }
}